package com.ronsicecream.pos.master.item;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeException;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;
import net.sourceforge.barbecue.output.OutputException;

public class PrintTwoColumn {

    private static String code;
    private static String item;

    public static void printBarcode(String noCode, String itemName) {

        PrinterJob pj = PrinterJob.getPrinterJob();
        if (pj.printDialog()) {
            PageFormat pf = pj.defaultPage();
            Paper paper = pf.getPaper();
            double width = fromCMToPPI(10);
            double height = fromCMToPPI(8.8);
            paper.setSize(width, height);
            paper.setImageableArea(
                    fromCMToPPI(0.25),
                    fromCMToPPI(0.5),
                    width - fromCMToPPI(0.35),
                    height - fromCMToPPI(1));
            System.out.println("Before- " + dump(paper));
            pf.setOrientation(PageFormat.PORTRAIT);
            pf.setPaper(paper);
            System.out.println("After- " + dump(paper));
            System.out.println("After- " + dump(pf));
            dump(pf);
            PageFormat validatePage = pj.validatePage(pf);
            System.out.println("Valid- " + dump(validatePage));
            //Book book = new Book();
            //book.append(new MyPrintable(), pf);
            //pj.setPageable(book);
            code = noCode;
            item = itemName;
            pj.setPrintable(new PrintTwoColumn.MyPrintable(), pf);
            try {
                pj.print();
            } catch (PrinterException ex) {
            }
        }
    }

    protected static double fromCMToPPI(double cm) {
        return toPPI(cm * 0.393700787);
    }

    protected static double toPPI(double inch) {
        return inch * 72d;
    }

    protected static String dump(Paper paper) {
        StringBuilder sb = new StringBuilder(64);
        sb.append(paper.getWidth()).append("x").append(paper.getHeight()).append("/").append(paper.getImageableX()).append("x").
                append(paper.getImageableY()).append(" - ").append(paper.getImageableWidth()).append("x").append(paper.getImageableHeight());
        return sb.toString();
    }

    protected static String dump(PageFormat pf) {
        Paper paper = pf.getPaper();
        return dump(paper);
    }

    public static class MyPrintable implements Printable {

        @Override
        public int print(Graphics graphics, PageFormat pageFormat,
                int pageIndex) throws PrinterException {
            System.out.println(pageIndex);
            int result = NO_SUCH_PAGE;
            if (pageIndex < 1) {
                try {
                    Barcode barcode = BarcodeFactory.createCode128C(code);
                    barcode.setResolution(72);
                    barcode.setFont(new Font("Arial", Font.BOLD, 0));
                    Graphics2D g2d = (Graphics2D) graphics;
                    System.out.println("[Print] " + dump(pageFormat));
                    int width = (int) pageFormat.getImageableWidth();
                    int height = (int) pageFormat.getImageableHeight();
                    g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
                    try {

                        g2d.drawImage(BarcodeImageHandler.getImage(barcode), 0, 1, (width / 2) - 1, (height / 5) - 5, null);
                        g2d.setFont(new Font("TimesRoman", Font.BOLD, 7));
                        g2d.drawString(code + " | THE SANDAL'S", 10, (height / 5) + 3);
                        g2d.setFont(new Font("TimesRoman", Font.BOLD, 7));
                        g2d.drawString(item, 10, (height / 5) + 10);

                        g2d.drawImage(BarcodeImageHandler.getImage(barcode), (width / 2) + 3, 1, (width / 2) - 1, (height / 4) - 5, null);
                        g2d.setFont(new Font("TimesRoman", Font.BOLD, 7));
                        g2d.drawString(code + " | THE SANDAL'S", (width / 2) + 14, (height / 5) + 4);
                        g2d.setFont(new Font("TimesRoman", Font.BOLD, 7));
                        g2d.drawString(item, (width / 2) + 14, (height / 5) + 11);
                    } catch (OutputException ex) {
                    }
                    result = PAGE_EXISTS;
                } catch (BarcodeException ex) {
                }

            }
            return result;
        }
    }
}